package cn.fl.npspl.controller;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.dto.BaseParamDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.framework.base.constant.WebBaseConstant;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.kingdeews.dto.KingDeeWsInfoDTO;
import cn.fl.npspl.service.ISplNpBscInfoService;
import cn.fl.npspl.service.ISplNpSocialRlService;
import cn.fl.npspl.vo.SplNpBscInfoVO;
import cn.fl.npspl.vo.SplNpSocialRlVO;
import cn.fl.product.service.ISplRlPsnService;
import cn.fl.product.vo.SplRlPsnVO;
import cn.fl.system.constant.SysLogConstant;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.alibaba.fastjson.TypeReference;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.constant.facade.framework.constant.SystemConstant;
import fl.customer.facade.customer.constant.CustomerConstant;
import fl.riskctrl.facade.riskctrl.riskctrlsingle.condition.RiskCtrlSingleInfo;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.framework.enums.SplOpStsCdEnum;
import fl.spl.facade.npspl.condition.SplNpBscInfoQC;

import fl.spl.facade.spl.api.ISplKingDeeWsInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 自然人经销商详细信息表 控制层
 */
@Controller
@RequestMapping("/npspl/splnpbscinfo")
public class SplNpBscInfoController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(SplNpBscInfoController.class);

    /**
     * SplNpBscInfo 服务层BEAN
     */
    @Autowired
    private ISplNpBscInfoService splNpBscInfoService;
    
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

	@Autowired
	private ISplKingDeeWsInfoService iKingDeeWsInfoService;
	@Autowired
	private ISplNpSocialRlService splNpSocialRlService;
	@Autowired
	private ISplRlPsnService splRlPsnService;
    /**
     * 到列表页面
     */
    @RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {

        ModelAndView view = new ModelAndView("npspl/splNpBscInfo/splNpBscInfoList");

        return view;
    }

    /**
     * 到新增修改页面
     */
    @RequestMapping(value = "/toAddPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {

        ModelAndView view = new ModelAndView("npspl/splNpBscInfo/splNpBscInfoAdd");

        return view;
    }

    /**
     * 到修改页面
     */
    @RequestMapping(value = "/toModifyPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {

        String id = request.getParameter("id");
        ModelAndView view = new ModelAndView("npspl/splNpBscInfo/splNpBscInfoModify");
        view.addObject("id", id);
        return view;
    }

    /**
     * 到详情页面
     */
    @RequestMapping(value = "/toDetailPage.do", method = {RequestMethod.GET})
    public ModelAndView toDetailPage(HttpServletRequest request, HttpServletResponse response) {

        String id = request.getParameter("id");
        ModelAndView view = new ModelAndView("npspl/splNpBscInfo/splNpBscInfoDetail");
        view.addObject("id", id);

        return view;
    }

    /**
     * 初始化列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initList(HttpServletRequest request, HttpServletResponse response) {

        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        String searchName = request.getParameter("demo.name");
        Pager page = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }

        if (StringUtils.isNotEmpty(pageSize)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }

        SplNpBscInfoQC qc = new SplNpBscInfoQC();
        if (StringUtils.isNotEmpty(searchName)) {
            //TODO SOMTHING
        }
        page.setCondition(qc);

        //TODO SOMETHING
        Pager pager = new Pager();//this.splNpBscInfoService.searchListPage(page);
        List<SplNpBscInfoVO> list = (List<SplNpBscInfoVO>) pager.getResultList();
        ListPageVO<SplNpBscInfoVO> listPage = new ListPageVO<SplNpBscInfoVO>(list, pager.getTotalCount());
        Long endTm = System.currentTimeMillis();
        return listPage;
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		SplNpBscInfoVO entity = new SplNpBscInfoVO();
		try {
			entity = this.getRequestParamForNpSpl(request);//获取参数
			entity.setCrtTm(new Date());
			entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
			entity.setOpStsCd(SplOpStsCdEnum.PASS.getStatus());
			entity.setDsCd(SplSystemConstant.FL_WEB_CHL_CD);
			Long id = this.splNpBscInfoService.add(entity);
			result.setData(id);
			BaseLogUtils.newWebLogger("新增供应商数据成功.DATA:"+JSONObject.toJSONString(entity)).setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.info();
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.newWebLogger("新增供应商数据失败,业务异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(ex)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.newWebLogger("新增供应商数据失败,系统异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(ex)
					.error(LogExceptionEnum.COMMON_CODE_0099);
			ex.printStackTrace();
		}

		return result;
	}

    /**
     * 修改
     */
    @RequestMapping(value = "/modify.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES,SysLogConstant.LOG_CMM_UPDATE_SUCC);
		SplNpBscInfoVO entity = new SplNpBscInfoVO();
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.newWebLogger("修改自然人供应商信息,主键不存在").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0001);
			return result;
		}
		try {
			entity = this.getRequestParamForNpSpl(request);
			entity.setId(Long.valueOf(id));
			entity.setMdfTm(new Date());
			entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
			boolean success = this.splNpBscInfoService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_UPDATE_FAIL);
				BaseLogUtils.newWebLogger("修改自然人供应商数据失败.DATA:"+JSONObject.toJSONString(entity)).setLogOperType(LogOperEnum.RESP)
						.setSysCd(CurrentThreadContext.getCurrentSysCd())
						.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
						.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
						.setWarning(false)
						.warn(LogExceptionEnum.COMMON_CODE_0002);
			}
			BaseLogUtils.newWebLogger("修改自然人供应商数据成功.DATA:"+JSONObject.toJSONString(entity)).setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.info();
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.newWebLogger("修改自然人供应商数据失败,业务异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(ex)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.newWebLogger("修改自然人供应商数据失败,系统异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(ex)
					.error(LogExceptionEnum.COMMON_CODE_0099);
		}
		return result;
	}

    /**
     * 删除数据
     */
    @RequestMapping(value = "/removeById.do", method = {RequestMethod.POST})
    @ResponseBody
    public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {

        String id = request.getParameter("id");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "删除成功!");
        if (StringUtils.isEmpty(id)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("主键不能为空!");
            return result;
        }
        try {
            this.splNpBscInfoService.removeById(Long.valueOf(id));
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("系统错误,请联系管理员!");
        }

        return result;
    }

    /**
     * 得到一条记录
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getById.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {

        String id = request.getParameter("id");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
        if (StringUtils.isEmpty(id)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("主键不能为空!");
			BaseLogUtils.newWebLogger("获取数据,主键不存在").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0001);
            return result;
        }

        try {
            SplNpBscInfoVO vo = this.splNpBscInfoService.selectById(Long.valueOf(id));
            if (null == vo) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("数据不存在!");
				BaseLogUtils.newWebLogger("获取数据,数据不存在").setLogOperType(LogOperEnum.RESP)
						.setSysCd(CurrentThreadContext.getCurrentSysCd())
						.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
						.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
						.setWarning(false)
						.warn(LogExceptionEnum.COMMON_CODE_0002);
            }
            result.setData(vo);
			BaseLogUtils.newWebLogger("获取供应商列表页面数据.DATA:"+JSONObject.toJSONString(result)).setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.info();
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
			BaseLogUtils.newWebLogger("根据主键获取对象失败,业务异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(ex)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.newWebLogger("根据主键获取对象失败,系统异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(ex)
					.error(LogExceptionEnum.COMMON_CODE_0099);
        }

        return result;
    }
    
    /**
	 * 获取自然人供应商参数信息
	 * @param request
	 * @return
	 * @throws BaseException
	 */
	private SplNpBscInfoVO getRequestParamForNpSpl(HttpServletRequest request)throws BaseException{
		String mfrIds = request.getParameter("mfrIds");//所属厂商
		List<Long> mfrIdsList = new ArrayList<Long>();
		if(StringUtils.isNotBlank(mfrIds)){
			String[] mfrIdsStr = mfrIds.split(";");
			if(mfrIdsStr.length>0){
				for (String str : mfrIdsStr) {
					mfrIdsList.add(Long.valueOf(str));
				}
			}
		}
		//String rebPct = request.getParameter("rebPct") ;//供应商返利比例
		String splNm = request.getParameter("splNm");//供应商名称
		String succ = request.getParameter("succ");// 身份证号
		String splTel = request.getParameter("splTel");//联系电话
		String regAddr = request.getParameter("regAddr");//户口所在地
		String postAddr = request.getParameter("postAddr");//通讯地址
		String zipNo = request.getParameter("zipNo");//邮编
		String rgnPrCd = request.getParameter("rgnPrCd");//区域 省CD
		String rgnCyCd = request.getParameter("rgnCyCd");//区域 市CD
		String atcId = request.getParameter("atcId");//所属交易市场
		String othInfoCd = request.getParameter("othInfoCd");//其他信息
		String rlPsnJson = request.getParameter("npSocialRlGridJSON");//社会成员/家庭成员
		
		String splAttrCd = request.getParameter("splAttrCd");//供应商属性

		String tkBrCd = request.getParameter("tkBrCd");//卡车品牌
		List<String> tkBrCdList = new ArrayList<String>();
		if(StringUtils.isNotEmpty(tkBrCd)){
			String[] brCdsStr = tkBrCd.split(";");
			if(brCdsStr.length>0){
				for (String str : brCdsStr) {
					tkBrCdList.add(str);
				}
			}
		}

		String carBrCd = request.getParameter("carBrCd");//乘用车品牌
		List<String> carBrCdList = new ArrayList<String>();
		if(StringUtils.isNotEmpty(carBrCd)){
			String[] brCdsStr = carBrCd.split(";");
			if(brCdsStr.length>0){
				for (String str : brCdsStr) {
					carBrCdList.add(str);
				}
			}
		}
		String busBrCd = request.getParameter("busBrCd");//客车品牌
		List<String> busBrCdList = new ArrayList<String>();
		if(StringUtils.isNotEmpty(busBrCd)){
			String[] brCdsStr = busBrCd.split(";");
			if(brCdsStr.length>0){
				for (String str : brCdsStr) {
					busBrCdList.add(str);
				}
			}
		}

		String prjTypCds = request.getParameter("prjTypCds");//项目类型
		List<String> prjTypCdsList = new ArrayList<String>();
		if(StringUtils.isNotEmpty(prjTypCds)){
			String[] prjTypCdsStr = prjTypCds.split(";");
			if(prjTypCdsStr.length>0){
				for (String str : prjTypCdsStr) {
					prjTypCdsList.add(str);
				}
			}
		}
		SplNpBscInfoVO splNpBscInfoVO = new SplNpBscInfoVO();

		if(StringUtils.isNotBlank(atcId)){
			splNpBscInfoVO.setAtcId(Long.valueOf(atcId));
		}

		//增加供应商属性判断
		if(StringUtils.isNotEmpty(splAttrCd) && CustomerConstant.CST_TYP_CD_NP.equals(splAttrCd)){
			if (StringUtils.isBlank(splNm)) {
				throw new BaseException(BaseExceptionConstant.COMMON_0001, "姓名不能为空!");
			}
			if (StringUtils.isBlank(succ)) {
				throw new BaseException(BaseExceptionConstant.COMMON_0001, "证件号不能为空!");
			}
			if (StringUtils.isEmpty(splTel)) {
				throw new BaseException(BaseExceptionConstant.COMMON_0001, "联系电话不能为空!");
			}
			if (!StringUtils.isMobilePhoneAndFixedPhone(splTel)) {
				throw new BaseException(BaseExceptionConstant.COMMON_0001, "请输入正确的电话号码!");
			}
			if (StringUtils.isEmpty(regAddr)) {
				throw new BaseException(BaseExceptionConstant.COMMON_0001, "户口所在地不能为空!");
			}
			if (StringUtils.isEmpty(postAddr)) {
				throw new BaseException(BaseExceptionConstant.COMMON_0001, "通讯地址不能为空!");
			}
			
			List<SplNpSocialRlVO> rlPsnList = new ArrayList<SplNpSocialRlVO>();
			if(null!=rlPsnJson){
				BaseLogUtils.newWebLogger("联系人信息.DATA:"+JSONObject.toJSONString(rlPsnJson)).setLogOperType(LogOperEnum.RESP)
						.setSysCd(CurrentThreadContext.getCurrentSysCd())
						.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
						.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
						.setWarning(false)
						.info();
				JSONArray parseArray = JSONObject.parseArray(rlPsnJson);
				for (int i = 0; i < parseArray.size(); i++) {
					JSONObject jsonObject = parseArray.getJSONObject(i);
					if (jsonObject.containsKey("__id")) {
						jsonObject.remove("__id");
					}
					SplNpSocialRlVO javaObject = JSONObject.toJavaObject(jsonObject, SplNpSocialRlVO.class);
					rlPsnList.add(javaObject);
				}
				//rlPsnList = JSONObject.parseArray(rlPsnJson, SplNpSocialRlVO.class);
				BaseLogUtils.newWebLogger("联系人信息.DATA:"+JSONObject.toJSONString(rlPsnList)).setLogOperType(LogOperEnum.RESP)
						.setSysCd(CurrentThreadContext.getCurrentSysCd())
						.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
						.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
						.setWarning(false)
						.info();
			}
			if(CollectionUtils.isEmpty(rlPsnList)){
				//新能源事业部 联系人不必填
				if(!PropertiesCacheUtils.getProperty(SystemConstant.FL_BUS_XNY_ORG_CD).equals(CurrentThreadContext.getCurrentBuOrgCd())){
					throw new BaseException(BaseExceptionConstant.COMMON_0001, "请维护主要人员信息.");
				}
			}else{
				BaseLogUtils.newWebLogger("校验联系人信息.DATA:"+JSONObject.toJSONString(rlPsnList)).setLogOperType(LogOperEnum.RESP)
						.setSysCd(CurrentThreadContext.getCurrentSysCd())
						.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
						.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
						.setWarning(false)
						.info();
				Map<String, String>  certTypCdList = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(),DictionaryConstant.CODE_RL_TYP_CD);
				
				for (SplNpSocialRlVO splRlPsnVO : rlPsnList) {
					
					if(StringUtils.isEmpty(splRlPsnVO.getRlNm())){
						throw new BaseException(BaseExceptionConstant.COMMON_0001, "请维护社会关系成员信息");
					}else if(StringUtils.isEmpty(splRlPsnVO.getRlTypCd())){
						throw new BaseException(BaseExceptionConstant.COMMON_0001, "联系人类型不能为空.");
					}
					if("1".equals(splRlPsnVO.getIsDefault()) && "10".equals(splRlPsnVO.getRlTypCd())){
						
						//验证输入项是否为空
						if(StringUtils.isEmpty(splRlPsnVO.getTel())){
							throw new BaseException(BaseExceptionConstant.COMMON_0001, certTypCdList.get(splRlPsnVO.getRlTypCd())+"电话不能为空.");
						}else if(StringUtils.isEmpty(splRlPsnVO.getCertTypCd())){
							throw new BaseException(BaseExceptionConstant.COMMON_0001, certTypCdList.get(splRlPsnVO.getRlTypCd())+"证件类型不能为空.");
						}else if(StringUtils.isEmpty(splRlPsnVO.getCertNo())){
							throw new BaseException(BaseExceptionConstant.COMMON_0001, certTypCdList.get(splRlPsnVO.getRlTypCd())+"证件号码不能为空.");
						}
					}
				}
				
				boolean isHaveActualPsn = false; //联系人

				for (SplNpSocialRlVO splRlPsnVO : rlPsnList) {
					if(null!=splRlPsnVO && "10".equals(splRlPsnVO.getRlTypCd())){
						isHaveActualPsn = true;
					}

					if(DictionaryConstant.CODE_CERT_TYP_CD_0.equals(splRlPsnVO.getCertTypCd())){
						//String rlTypCdNu=certTypCdList.get(splRlPsnVO.getRlTypCd());
						//this.validateIdCard(rlTypCdNu,splRlPsnVO.getRlNm(),splRlPsnVO.getCertNo());
					}

				}
				if(!isHaveActualPsn){
					throw new BaseException(BaseExceptionConstant.COMMON_0001, "请维护主要联系人.");
				}
			}
			if(CollectionUtils.isNotEmpty(rlPsnList)){
				for (SplNpSocialRlVO splRlPsnVO : rlPsnList) {
					if(StringUtils.isEmpty(splRlPsnVO.getIsDel())){
						splRlPsnVO.setIsDel(BaseConstant.IS_YESNO_NO);
					}
					if(StringUtils.isEmpty(splRlPsnVO.getId())){
						splRlPsnVO.setIsDel(BaseConstant.IS_YESNO_NO);
						splRlPsnVO.setCrtTm(new Date());
						splRlPsnVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
					}else{
						splRlPsnVO.setMdfTm(new Date());
						splRlPsnVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
					}
				}
			}
			BaseLogUtils.newWebLogger("联系人信息.DATA:"+JSONObject.toJSONString(rlPsnList)).setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.info();
			splNpBscInfoVO.setRlPsnList(rlPsnList);
		}
		
		splNm = org.springframework.util.StringUtils.replace( splNm, "（", "(" );
		splNm = org.springframework.util.StringUtils.replace( splNm, "）", ")");

		splNpBscInfoVO.setCstNm(splNm.trim());
		splNpBscInfoVO.setCertNo(succ);//身份证号
		splNpBscInfoVO.setMp1(splTel);
		if(StringUtils.isNotEmpty(regAddr)){
			splNpBscInfoVO.setIdAddr(regAddr.trim());
		}
		splNpBscInfoVO.setPostAddr(postAddr);
		splNpBscInfoVO.setZipNo(zipNo);//邮编
		if(CollectionUtils.isNotEmpty(prjTypCdsList) && prjTypCdsList.size() > 0){
			splNpBscInfoVO.setPrjTypCds(prjTypCdsList);//项目类型
		}
		splNpBscInfoVO.setTkBrCdList(tkBrCdList);//卡车品牌
		splNpBscInfoVO.setCarBrCdList(carBrCdList);//乘用车品牌
		splNpBscInfoVO.setBusBrCdList(busBrCdList);//客车品牌
		splNpBscInfoVO.setCstRem(othInfoCd);
		splNpBscInfoVO.setMfrIds(mfrIdsList);//所属厂商
		if(StringUtils.isNotEmpty(rgnPrCd)){
			splNpBscInfoVO.setRgnPrCd(rgnPrCd.trim());
		}
		if(StringUtils.isNotEmpty(rgnCyCd)){
			splNpBscInfoVO.setRgnCyCd(rgnCyCd.trim());
		}

		splNpBscInfoVO.setBuOrgCds(Arrays.asList(CurrentThreadContext.getCurrentBuOrgCd()));//所属事业部

		splNpBscInfoVO.setCstTypCd(splAttrCd);//供应商属性

		return splNpBscInfoVO;
	}
	
	/**
     * 征信查询
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/checkCredit.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    @Deprecated
    public Object checkCredit(HttpServletRequest request, HttpServletResponse response) throws BaseException {

        String splNm = request.getParameter("splNm");//经销商名称-自然人即客户姓名
        String certNo = request.getParameter("certNo");// 证件号
        String certTypCd = request.getParameter("certTypCd");// 证件类型(自然人默认为0-身份证)
        String mp = request.getParameter("mp");// 手机号
        String splAttrCd = request.getParameter("splAttrCd");// 客户类型(供应商属性)
        String chlCd = SplSystemConstant.FL_WEB_CHL_CD;
        
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
        if (StringUtils.isEmpty(splNm)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("主键不能为空!");
			BaseLogUtils.newWebLogger("获取数据,主键不存在").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0001);
            return result;
        }
        
        RiskCtrlSingleInfo info = new RiskCtrlSingleInfo();
        info.setCstNm(splNm);
        if (StringUtils.isEmpty(certTypCd)) {
			info.setCertTypCd(certTypCd);
		} else {
			info.setCertTypCd(BaseConstant.IS_YESNO_NO + "");
		}
        info.setCertNo(certNo);
        info.setMp1(mp);
        info.setCstTypCd(splAttrCd);
        // TODO 客户经理信息?
        info.setDsCd(chlCd);
        info.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
        info.setCrtNm(CurrentThreadContext.getCurrentUserName());
        
        String ip = "";
        
        BaseParamDTO baseParamDto = new BaseParamDTO();
        baseParamDto.setSysCd(WebBaseConstant.SYS_SYS_CD);
        baseParamDto.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD);
        baseParamDto.setServiceCd(WebBaseConstant.FL_SERVICE_SYS_CD);
        
		try {
            /*DataResultDTO checkCredit = this.splNpBscInfoService.checkCredit(info, ip, baseParamDto);
            if (null == checkCredit || null == checkCredit.getData()) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("未查询到数据");
                BaseLogUtils.info(logger, "checkCredit", "获取数据,数据不存在.[splNm=" + splNm + "]", CurrentThreadContext.getCurrentUserName());
            }
            result.setData(checkCredit.getData());*/
			BaseLogUtils.newWebLogger("获取数据.DATA:"+JSONObject.toJSONString(result)).setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.info();
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
			BaseLogUtils.newWebLogger("根据主键获取对象失败,业务异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(ex)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.newWebLogger("根据主键获取对象失败,系统异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(ex)
					.error(LogExceptionEnum.COMMON_CODE_0099);
        }

        return result;
    }

	/**
	 * 校验是否是狮桥员工
	 */
	@RequestMapping(value = "/kingDeeWsCheck.do", method = {RequestMethod.POST})
	@ResponseBody
	public Object kingdeeWsCheck(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "获取成功");
		BaseLogUtils.newWebLogger("开始校验是否是狮桥员工：kingdeeWsCheck").setLogOperType(LogOperEnum.RESP)
				.setSysCd(CurrentThreadContext.getCurrentSysCd())
				.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
				.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
				.setWarning(false)
				.info();
		String npSocialRlGridJSON = request.getParameter("npSocialRlGridJSON");
		String splAttrCd = request.getParameter("splAttrCd");//供应商属性
		List<SplRlPsnVO> rlPsnList = new ArrayList<SplRlPsnVO>();
		if(null!=npSocialRlGridJSON){
			rlPsnList = JSON.parseArray(npSocialRlGridJSON, SplRlPsnVO.class);
		}
		if( rlPsnList.isEmpty() ){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("联系人列表为空");
			return request;
		}
		//联系人类型
		Map<String,String> map = new HashMap<>();
		//联系人类型
		List<KeyValueVO> rlTypCDList;
		if ("1".equals(splAttrCd)) {
			rlTypCDList = this.splNpSocialRlService.searchRlTypCdList(DictionaryConstant.CODE_GRP_RL_TYP_CD);
		} else {
			rlTypCDList = this.splRlPsnService.searchRlTypCdList();
		}
		for(KeyValueVO kv : rlTypCDList  ){
			map.put(kv.getKey(),kv.getValue());
		}
		String resultPhone ="";
		for( SplRlPsnVO rlInfo : rlPsnList ){
			resultPhone = iKingDeeWsInfoService.getPhoneNum(rlInfo.getTel());
			if( StringUtils.isBlank(resultPhone) ){
//				result.setSuccess(BaseConstant.IS_YESNO_NO);
//				result.setInfo("金蝶接口调用失败");
				return result;
			}
			BaseLogUtils.newWebLogger("获取resultPhone："+resultPhone).setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.info();
			ArrayList<KingDeeWsInfoDTO> infos = JSON.parseObject(resultPhone, new TypeReference<ArrayList<KingDeeWsInfoDTO>>(){});
			if( infos.size()>1 ){//证明有多个事业部
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("["+map.get(rlInfo.getRlTypCd())+":"+rlInfo.getRlNm()+"]"+"手机号/证件号为狮桥内部员工："+infos.get(0).getDepartmentName()+"/"+infos.get(0).getUserName()+"，禁止维护，请重新填写");
				return result;
			}else {
				for ( KingDeeWsInfoDTO info : infos){
					if( StringUtils.isNotBlank(info.getUserCode()) ) {
						result.setSuccess(BaseConstant.IS_YESNO_NO);
						result.setInfo("["+map.get(rlInfo.getRlTypCd())+":"+rlInfo.getRlNm()+"]"+"手机号/证件号为狮桥内部员工：" + info.getDepartmentName() + "/" + info.getUserName() + "，禁止维护，请重新填写");
						return result;
					}
				}
			}
		}
		String resultIdCard ="";
		for( SplRlPsnVO rlInfo : rlPsnList ){
			resultIdCard = iKingDeeWsInfoService.getIdentityNum(rlInfo.getCertNo());
			if( StringUtils.isBlank(resultIdCard) ){
//				result.setSuccess(BaseConstant.IS_YESNO_NO);
//				result.setInfo("金蝶接口调用失败");
				return result;
			}
			BaseLogUtils.newWebLogger("获取resultIdCard："+resultIdCard).setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.info();
			ArrayList<KingDeeWsInfoDTO> infos = JSON.parseObject(resultIdCard, new TypeReference<ArrayList<KingDeeWsInfoDTO>>(){});
			if( infos.size()>1 ){//证明有多个事业部
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("["+map.get(rlInfo.getRlTypCd())+":"+rlInfo.getRlNm()+"]"+"手机号/证件号为狮桥内部员工："+infos.get(0).getDepartmentName()+"/"+infos.get(0).getUserName()+"，禁止维护，请重新填写");
				return result;
			}else {
				for ( KingDeeWsInfoDTO info : infos){
					if( StringUtils.isNotBlank(info.getUserCode()) ) {
						result.setSuccess(BaseConstant.IS_YESNO_NO);
						result.setInfo("["+map.get(rlInfo.getRlTypCd())+":"+rlInfo.getRlNm()+"]"+"手机号/证件号为狮桥内部员工：" + info.getDepartmentName() + "/" + info.getUserName() + "，禁止维护，请重新填写");
						return result;
					}
				}
			}
		}
		return result;
	}
}

